package top.kaoshanji.p1ch01a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/9 18:42
 */
public class FilteringApples {

    private static final Logger logger = LogManager.getLogger(FilteringApples.class);

    public static void main(String[] args) {

        List<Apple> inventory = Arrays.asList(
                new Apple(80, "green"),
                new Apple(155, "green"),
                new Apple(120, "red")
        );


        // 代码清单-p1ch01a-12页c：使用方法引用方式筛选颜色为绿色的苹果
        List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
        logger.info("...筛选颜色为绿色的苹果：{}...", greenApples);

        // 代码清单-p1ch01a-12页d：使用方法引用方式筛选重量大于150的苹果
        List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple);
        logger.info("...筛选重量大于150的苹果：{}...", heavyApples);


        // 代码清单-p1ch01a-13页a：使用Lambda方式筛选颜色为绿色的苹果
        List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
        logger.info("...筛选颜色为绿色的苹果：{}...", greenApples2);

        // 代码清单-p1ch01a-13页b：使用Lambda方式筛选重量大于150的苹果
        List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
        logger.info("...筛选重量大于150的苹果：{}...", heavyApples2);

        // 代码清单-p1ch01a-13页c：使用Lambda方式筛选重量小于150或者颜色为brown的苹果
        List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 || "brown".equals(a.getColor()));
        logger.info("...筛选重量小于150或者颜色为brown的苹果：{}...", weirdApples);


        // 代码清单-p1ch01a-17页a：基于流式筛选重量大于150的苹果
        List<Apple> heavyApples3 = inventory.stream().filter((Apple a) -> a.getWeight() > 150).collect(Collectors.toList());
        logger.info("...筛选重量大于150的苹果：{}...", heavyApples3);

        // 代码清单-p1ch01a-17页b：基于并行流筛选重量大于150的苹果
        List<Apple> heavyApples4 = inventory.parallelStream().filter((Apple a) -> a.getWeight() > 150).collect(Collectors.toList());
        logger.info("...筛选重量大于150的苹果：{}...", heavyApples4);
    }


    // 代码清单-p1ch01a-12页a：Java8之前筛选颜色为绿色的苹果
    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            if ("green".equals(apple.getColor())) {
                result.add(apple);
            }
        }
        return result;
    }

    // 代码清单-p1ch01a-12页b：Java8之前筛选重量大于150的苹果
    public static List<Apple> filterHeavyApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.getWeight() > 150) {
                result.add(apple);
            }
        }
        return result;
    }






    /**
     * 返回符合过滤条件的苹果
     * @param appleList 苹果集合
     * @param p Predicate 筛选条件
     * @return List<Apple>
     */
    public static List<Apple> filterApples(List<Apple> appleList, Predicate<Apple> p) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: appleList) {
            // 过滤符合条件的数据
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    // 过滤条件：颜色为绿色
    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    // 过滤条件：重量大于150
    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 150;
    }




}
